package com.quicks.beauty.server.config;

import com.quicks.beauty.server.common.AjaxResult;
import org.springframework.boot.web.servlet.error.ErrorController;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

@RestController
class MainSiteErrorController implements ErrorController {

    @RequestMapping("/error")
    public AjaxResult handleError(HttpServletRequest request) {
        AjaxResult result = new AjaxResult();

//        Map<String, Object> body = getErrorAttributes(request, isIncludeStackTrace(request, MediaType.TEXT_HTML));
//        HttpStatus status = getStatus(request);
//        return new ResponseEntity<Map<String, Object>>(body, status);
        //获取statusCode:401,404,500
        Integer statusCode = (Integer) request.getAttribute("javax.servlet.error.status_code");
        String pathInfo = request.getPathInfo();
        System.out.println("pathInfo = " + pathInfo);
        if (statusCode == 401) {
            result.setSuccess(false);
            result.setMsg("路径权限不允许");
            return result;
        } else if (statusCode == 404) {
            result.setSuccess(false);
            result.setMsg("路径不存在");
            return result;
        } else if (statusCode == 403) {
            result.setSuccess(false);
            result.setMsg("路径不允许");
            return result;
        } else {
            result.setSuccess(false);
            result.setMsg("服务器打盹中...");
            return result;
        }

    }

    @Override
    public String getErrorPath() {
        return "/error";
    }
}
